package com.swgk.core.view.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.swgk.core.BR;
import com.swgk.core.R;
import com.swgk.core.view.entity.MenuEntity;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.recyclerview.adapter.PageRecycleAdapter;

import java.util.List;

public class MenuAdapter extends PageRecycleAdapter<MenuEntity> {

    private int resID;//本地资源id
    public boolean onlyimage = false;
    RequestOptions options = new RequestOptions().placeholder(R.mipmap.image_qs)//图片加载出来前，显示的图片
            .fallback(R.mipmap.image_qs) //url为空的时候,显示的图片
            .error(R.mipmap.image_qs);//图片加载失败后，显示的图片

    public MenuAdapter(Context context, List<MenuEntity> datas) {
        super(context, R.layout.item_menu, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, MenuEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        ImageView iv = holder.itemView.findViewById(R.id.iv);
        ImageView iv_2 = holder.itemView.findViewById(R.id.iv_2);
        ImageView iv_3 = holder.itemView.findViewById(R.id.iv_3);
        ImageView iv_4 = holder.itemView.findViewById(R.id.iv_4);
        TextView tv = holder.itemView.findViewById(R.id.tv);

        tv.setVisibility(onlyimage ? View.GONE : View.VISIBLE);
        iv_4.setVisibility(onlyimage ? View.VISIBLE : View.GONE);

        if (onlyimage) {
            iv_3.setVisibility(View.GONE);
            iv_2.setVisibility(View.GONE);
            iv.setVisibility(View.GONE);
            Glide.with(context).load(entity.getAppIcon() + "").apply(options).into(iv_4);
        } else {
            if (!TextUtils.isEmpty(entity.getMurl()) && entity.getMurl().contains("http")) {
                iv.setVisibility(View.GONE);
                iv_2.setVisibility(View.GONE);
                iv_3.setVisibility(View.VISIBLE);
                Glide.with(context).load(entity.getMurl() + "")
                        .apply(RequestOptions.bitmapTransform(new CircleCrop()).placeholder(R.mipmap.default_user))
                        .into(iv_3);


            } else if (entity.getAppIcon().contains("http")) {
                iv_2.setVisibility(View.GONE);
                iv_3.setVisibility(View.GONE);
                iv.setVisibility(View.VISIBLE);

            } else {
                //判断是否存在
                if (!TextUtils.isEmpty(entity.getUrl())) {
                    iv_2.setVisibility(View.GONE);
                    iv_3.setVisibility(View.GONE);
                    iv.setVisibility(View.VISIBLE);

                    //获取到对应的本地图标地址id
                    resID = context.getResources().getIdentifier(entity.getUrl(), "drawable", context.getPackageName());
                    if (resID != 0) {
                        //Glide加载本地的图标图片iv
                        Glide.with(context).load(resID).into(iv);
                    }
                } else {
                    iv_3.setVisibility(View.GONE);
                    iv_2.setVisibility(View.VISIBLE);
                    iv.setVisibility(View.GONE);
                }
            }
        }
    }


    public void onlyImage(boolean type) {
        this.onlyimage = type;
    }
}
